package com.edp.expandable;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.edp.buscabus.MainActivity_;
import com.edp.buscabus.R;

public class ExpandableListViewAppActivity extends Activity{
	 
	private ExpandableListAdapterV adapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result);
     // Retrive the ExpandableListView from the layout
        ExpandableListView listView = (ExpandableListView) findViewById(R.id.listView);
        
        listView.setOnChildClickListener(new OnChildClickListener(){
            @Override
            public boolean onChildClick(ExpandableListView arg0, View arg1, int arg2, int arg3, long arg4){
                TextView nome = (TextView)arg1.findViewById(R.id.tvDescida);
            	String cod = nome.getText().toString();
            	cod = cod.substring(0, cod.indexOf("-"));
            	Intent i = new Intent(getApplicationContext(), MainActivity_.class);
            	try{
	            	int iCod = Integer.parseInt(cod);
					i.putExtra("cod", iCod);
	            	startActivity(i);
            	}catch(Exception e){
            		
            	}
//				Toast.makeText(getBaseContext(), "Child clicked - " + cod , Toast.LENGTH_LONG).show();
                return false;
            }
        });
	        
        listView.setOnGroupClickListener(new OnGroupClickListener(){
		   @Override
		   public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
		    // TODO Auto-generated method stub
		    Toast.makeText(getBaseContext(), "Group clicked", Toast.LENGTH_LONG).show();
		    return false;
		   }
        });

        ArrayList<String> rotas = new ArrayList<String>();
		ArrayList<ArrayList<String>> childrens = new ArrayList<ArrayList<String>>();
		
        Bundle extras = getIntent().getExtras();
        if(extras.containsKey("children")){
        	ArrayList<String> children = extras.getStringArrayList("children");
        	int c = 0;
        	for (String s : children) {	//"Origem:60,80;PARANGABA:89,38"
				c++;
				rotas.add("Rota " + c);				
				ArrayList<String> l = new ArrayList<String>();
				for (String string : s.split(";"))
					l.add(string);
				childrens.add(l);
			}
        }
        // Initialize the adapter with blank groups and children
        // We will be adding children on a thread, and then update the ListView
		adapter = new ExpandableListAdapterV(this, rotas, childrens);
        listView.setAdapter(adapter);
        
    }
	 
}